package io.zhengsh.juc._1lock.atomic.aba;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicStampedReference;

public class ABATest2 {

    private static AtomicStampedReference index = new AtomicStampedReference(10, 1);

    public static void main(String[] args) {
        new Thread(() -> {
            index.compareAndSet(10, 101, index.getStamp(), index.getStamp() + 1);
            index.compareAndSet(101, 10, index.getStamp(), index.getStamp() + 1);
            System.out.println(Thread.currentThread() + ": 10->101->10");
        }, "Tom").start();

        int stamp = index.getStamp();
        new Thread(() -> {

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean result = index.compareAndSet(10, 1000, stamp, stamp + 1);
            System.out.println(Thread.currentThread() + ": 更新结果：" + result + ", 更新后的值: " + index.getReference());
        }, "Jack").start();
    }
}
